#!/bin/sh

# bc needs scale 2
# GiB unit assumed
SCALE=2
start_cache_size=0.25
end_cache_size=32.00

policy="lru icc lef2 ic"
sesslen="full beta"
interseek="noseek lognormal"

arrival_rates="0.1 0.2"

(
for a in $arrival_rates
do
    for p in $policy
    do
	for l in $sesslen
	do
	    for is in $interseek
	    do
		s=`echo "scale=$SCALE;($start_cache_size + $end_cache_size) / 2" | bc`
		s=`printf "%.${SCALE}f" $s`
		up=$end_cache_size
		down=$start_cache_size
		last_good_logbase=
		
		while true
		do
		    echo "trying $p with ${s}GiB"
		    logbase=log_${p}_${l}_${is}_${s}GiB
		    ./ms2sim $conf -a ${a}/m -s ${s}GiB -m $p -l $logbase -g $l -p $is
		    res=$?
		    if [ $res -eq 0 ]; then
			up=$s
			if [ ! -z $last_good_logbase ]; then
			    rm -f ${last_good_logbase}_*.log
			fi
			last_good_logbase=$logbase
		    else
			down=$s
		    fi
		    s=`echo "scale=$SCALE;($up + $down) / 2" | bc`
		    s=`printf "%.${SCALE}f" $s`
		    if [ $s == $up ] || [ $s == $down ]; then
			break;
		    fi
		done
	    done
	done
    done

    mkdir good_logs_${a}
    mv *.log good_logs_${a}
done
exit
) 2>&1 | tee log.txt
